var tb;
function initData(stationId) {
	$.getJSON("/rest/protectAlarm/getProtectionStatusByStationId/" + stationId,
			function(result) {
				loadTable(result);
			});
}
function loadTable(data) {
	var message1 = "";
	var dataset = [];
	for ( var i in data) {
		var d = data[i];
		dataset.push([ d['ptId'], d['name'], d['type'], d['protectedName'],
				d['substationName'], getStatus(d['mCommuStatus']),
				getStatus(d['mRunStatus']) ]);
	}

	var myOpt = MyTable.getDefaultOptions();
	myOpt.columnDefs = [ {
		"targets" : [ 0 ],
		"visible" : false
	} ];
	myOpt.paging = true;
	myOpt.pageLength = 9;
	tb = new MyTable('statusTable', [ '保护id', '保护名称', '保护类型', '被保护设备', '变电站',
			'通讯状态', '运行状态' ], myOpt);
	tb.setDataSet(dataset);
	tb.show();
	// 鼠标滑过事件
	$('#statusTable tbody').on(
			'mouseover',
			'tr',
			function() {
				var table = $('#statusTable').DataTable();
				var selectedData = null;
				if ($(this).hasClass('selected')) {
					$(this).removeClass('selected');
				} else {
					table.$('tr.selected').removeClass('selected');
					$(this).addClass('selected');
				}
				selectedData = table.row('.selected').data();
				if (typeof (selectedData) != "undefined") {
					var privateId = selectedData[0];
				}
				$.getJSON("/rest/protectAlarm/getChannelStatusByPrivateId/"
						+ privateId, function(result) {
					 message1 = drawTitleSpan(result);
				});
				$.getJSON("/rest/protectAlarm/getDevidesStatusByPrivateId/"
						+ privateId, function(result) {
					drawDetialSpan(result,message1);
				});
			}).on('mouseleave', 'tr', function() {
		var table = $('#statusTable').DataTable();
		table.$('tr.selected').removeClass('selected');
		document.getElementById('messaageDiv').innerHTML = "";
		document.getElementById('messaageDiv2').innerHTML = "";
	});
}
function getString(data) {
	if (data) {
		switch (data.type) {
		case 'dayTimeStr':
			var t = new Date(data.value);
			var m = t.getMonth() + 1 > 9 ? (t.getMonth() + 1) : ("0" + (t
					.getMonth() + 1));
			var d = t.getDate() > 9 ? t.getDate() : "0" + t.getDate();
			return t.getFullYear() + "-" + m + "-" + d;
		default:
			return data;
		}
	} else {
		return "";
	}
}
function getStatus(value) {
	var s = '';
	switch (value) {
	case -1:
		s = '<span style="color:red"><b>异常</b></span>';
		break;
	default:
		s = '<span >正常</span>';
	}
	return s;
}

function drawTitleSpan(result) {
	var data = result["0"];
	var message = "";
	if (data != null) {
		var time = getTimeStr(data.triggerTime.time);
		var c = data.alarmChannel;
		message = '<h4><i class="icon fa fa-warning"></i>通讯异常</h4><h4>'
				+ time
				+ "</h4><span style=''>告警对象："
				+ data.alarmObject
				+ '<br/> 内容：'
				+ c.substring(c.indexOf("通道") == -1 ? 0 : c.indexOf("通道"),
						c.length) + "</span>发生<span style='margin:5px'>"
				+ data.name + "</span>";
	}
	document.getElementById('messaageDiv').innerHTML = message;
	return message;
}

function drawDetialSpan(result,message1) {
	var data = result["0"];
	if (data != null) {
		var message = '<h4><i class="icon fa fa-warning"></i>运行异常</h4><h4>'
				+ getTimeStr(data.alarm_time)
				+ '</h4></span>内容：<span style="margin:5px;">' + data.alarm_name
				+ "</span>";
		if (message1 = "") {
			document.getElementById('messaageDiv').innerHTML = message;
		} else {
			document.getElementById('messaageDiv2').innerHTML = message;
		}
	}
}

function getTimeStr(millis) {
	var time = new Date(millis);
	return time.getFullYear() + '年' + (time.getMonth() + 1) + '月'
			+ time.getDate() + '日 ' + time.getHours() + '时' + time.getMinutes()
			+ '分' + time.getSeconds() + '秒';
}